﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class fosterController : ControllerBase
    {
        private readonly PetDbContext db;
        public fosterController(PetDbContext _context)
        {
            db = _context;

        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            var resultData = db.Pet_foster.ToList<Pet_foster>();
            //result.Data = resultData;
            //result.Code = 200;
            //result.Name = "1598033581@qq.com";
            return result;
        }

        /// <summary>
        /// 用id查询数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public Result Get(Guid Id)
        {
            Result result = new Result();
            Pet_foster? resulrData = db.Pet_foster.Where(w => w.ID == Id).FirstOrDefault<Pet_foster>();
            //if (resulrData == null)
            //{
            //    result.Code = 404;
            //}
            //else
            //{
            //    result.Code = 200;
            //    result.Data = resulrData;
            //}
            return result;
        }

        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="foster"></param>
        /// <returns></returns>

        [HttpPost]
        public Result Add([FromBody] Pet_foster foster)
        {
            Result result = new Result();
            db.Pet_foster.Add(foster);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_foster? foster = db.Pet_foster.Where(w => w.ID == id).SingleOrDefault();
            if (foster != null)
                db.Pet_foster.Remove(foster);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="foster"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Up([FromBody] Pet_foster foster)
        {
            Result result = new Result();
            db.Pet_foster.Update(foster);
            result.Data = db.SaveChanges();
            return result;
        }
    }
}
